/**
 * Created by liuqingxia on 2016/5/30.
 */

/**
 * 执行类型
 * @type {{ONCE: number, CRON: number, isOnce: Function, isCron: Function, getTypeStr: Function}}
 */
var ExecuteType = {
    ONCE: 1,
    CRON: 0,
    isOnce: function (value) {
        if (value == 1) {
            return true;
        }
        return false;
    },
    isCron: function (value) {
        if (value == 0) {
            return true;
        }
        return false;
    },
    getTypeStr: function (value) {
        if (value == 1) {
            return "执行一次";
        }
        if (value == 0) {
            return "定时";
        }
        return "未知";
    }
};


/**
 * 选择执行类型
 */
function selectExecuteTypeRadio() {
    $(document).on("click", "input[name='executeType']", function () {
        var value = parseInt($(this).val());
        if (ExecuteType.isCron(value)) {
            $("#cron-generate-row").show();
        }
        if (ExecuteType.isOnce(value)) {
            $("#cron-generate-row").hide();
        }
    });
}

/**
 *初始化ExecuteTypeRadio
 */
function initExecuteTypeRadio(value) {
    if (ExecuteType.isCron(value)) {
        $("input[name='executeType']").eq(0).removeAttr("checked");
        $("input[name='executeType']").eq(1).attr("checked", "checked");
        $("input[name='executeType']").eq(1).click();
        $("#cron-generate-row").show();
        return;
    }
    if (ExecuteType.isOnce(value)) {
        $("input[name='executeType']").eq(1).removeAttr("checked");
        $("input[name='executeType']").eq(0).attr("checked", "checked");
        $("input[name='executeType']").eq(0).click();
        $("#cron-generate-row").hide();
        return;
    }
}

/**
 * 获取executeType及cronExpression
 * @param params
 * @returns {*}
 */
function getExecuteTypeParams(params) {
    var val = parseInt($('input:radio[name="executeType"]:checked').val());
    if (ExecuteType.isCron(val)) {
        var executeType = ExecuteType.CRON;
        var cronExpression = $("#cronExpression").val();
        if (isNull(cronExpression)) {
            swal("警告", "请输入cronExpression", "warning");
            return false;
        }
        params['cronExpression'] = cronExpression;
        params['executeType'] = executeType;
    }
    if (ExecuteType.isOnce(val)) {
        var executeType = ExecuteType.ONCE;
        params['cronExpression'] = "";
        params['executeType'] = executeType;
    }
    return params;
}
